<!-- 逻辑
	1.进入页面判断本地是否存在用户信息，有则跳转；
	2.
 -->
<template>
	<view class="">
		<view class="login-box">
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="用户名" name="userName" class="login-input">
					<uni-easyinput class=" login-id" type="text" v-model="formData.userName" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item label="账号" name="id" class="login-input">
					<uni-easyinput class=" login-id" type="text" v-model="formData.id" placeholder="请输入账号" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="password" class="login-input">
					<uni-easyinput class="login-password" type="password" v-model="formData.password"
						placeholder="请输入密码" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="password2" class="login-input">
					<uni-easyinput class="login-password" type="password" v-model="formData.password2"
						placeholder="请再次输入密码" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" class="login-btn" @click="submit">注册</button>
		</view>
	</view>
</template>

<script>
	import {
		callCloudFunction
	} from '@/api/requests.js'
	export default {
		data() {
			return {
				formData: {
					id: '',
					userName: '',
					password: '',
					password2: ''
				},
				rules: {
					userName: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名',
						}, ]
					},
					id: {
						rules: [{
							required: true,
							errorMessage: '请输入账号',
						}, ]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码',
						}, ]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请再次输入密码',
						}, ]
					},
					password2: {
						rules: [{
							required: true,
							errorMessage: '请再次输入密码',
						}, ]
					}
				}
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					const {
						id,
						userName,
						password,
						password2
					} = this.formData
					if (password === password2) {
						this.handleRegister(userName, id, password)
					} else {
						uni.showModal({
							content: `两次输入密码不一致`,
							showCancel: false
						})
					}
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			handleRegister(userName, id, password) {
				const data = {
					id,
					userName,
					password
				}
				callCloudFunction('register', data, res => {
						if (res.result.code != 0) {
							uni.showModal({
								content: res.result.message,
								showCancel: false
							})
						} else {
							uni.showModal({
								content: `注册成功`,
								showCancel: false
							})
							uni.setStorage({
								key: 'userInfo',
								data: res.result.data.data[0],
							});
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/add/add'
								});
							}, 2000)
						}
					},
					err => {
						uni.showModal({
							content: `注册失败`,
							showCancel: false
						})
					})
			}
		},
	}
</script>

<style>
	.login-input {
		margin: 10px;
	}

	.login-btn {
		width: 100px;
		height: 40px;
		margin: 0 auto;
		line-height: 40px;
	}
</style>
